package org.testis.orm.refinedclasses;

import java.util.HashMap;
import org.orm.PersistentException;
import org.testis.orm.classes.T_test_stepcontainer;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.utils.middleware.TestDesign;

/**
 * 
 * @author Cahit Emir Erel
 */
public class PreConditionCaseRefined extends DesignPart {

    private T_test_stepcontainer m_reference;

    public PreConditionCaseRefined(T_test_stepcontainer reference) {
	setReference(reference);
    }

    synchronized public PreConditionCaseRefined createRefinedDatabaseCopy()
	    throws PersistentException {
	return new PreConditionCaseRefined(createDatabaseCopy());
    }

    private T_test_stepcontainer createDatabaseCopy()
	    throws PersistentException {
	T_test_stepcontainer newContainer = new T_test_stepcontainer();
	newContainer.setContainerTypeId(this.getReference()
		.getContainerTypeId());
	newContainer.setDescription(this.getReference().getDescription());
	newContainer.setName(this.getReference().getName());
	if (newContainer.save()) {
	    return newContainer;
	} else {
	    throw new PersistentException(
		    "Unable to create database copy of PreCondition Case: "
			    + this);
	}
    }

    @Override
    public String toString(TestDesignTreeNode treeNode) {
	try {
	    StringBuffer buffer = new StringBuffer();
	    buffer.append(this.toString());
	    Boolean val = treeNode.getReference().getParameterBooleanValue(
		    TestDesignTreeParameters.Critical.name());
	    if (val != null) {
		buffer.append("\nCritical: " + val);
	    }
	    return buffer.toString();
	} catch (NullPointerException ex) {
	    ex.printStackTrace();
	    return null;
	}
    }

    @Override
    public String toString() {
	try {
	    StringBuffer buffer = new StringBuffer();
	    buffer.append("Step Container Id: " + this.getId());
	    buffer.append("\nType: "
		    + TestDesign.getDesignPartTypeName(this.getReference()
			    .getContainerTypeId()));

	    buffer.append("\nName: " + this.getName());
	    buffer.append("\nDescription: "
		    + this.getReference().getDescription());
	    return buffer.toString();
	} catch (NullPointerException ex) {
	    ex.printStackTrace();
	    return null;
	}
    }

    public T_test_stepcontainer getReference() {
	return m_reference;
    }

    /**
     * 
     * @param reference
     * @throws InstantiationError
     * @throws NullPointerException
     */
    private void setReference(Identifier reference) {
	if (reference == null) {
	    throw new NullPointerException();
	}
	if (reference instanceof T_test_stepcontainer) {
	    m_reference = ((T_test_stepcontainer) reference);
	} else {
	    throw new InstantiationError();
	}
    }

    /**
     * 
     * @return
     * @throws NullPointerException
     */
    public String getTypeName() {
	return TestDesign.getDesignPartTypeName(this.getReference()
		.getContainerTypeId());
    }

    private int compare(Comparable source) {
	return Comparable.EXACT;
    }

    public int compare(Comparable source, Comparable treeReference) {
	TestDesignTreeNode src = (TestDesignTreeNode) source;
	TestDesignTreeNode thisTree = (TestDesignTreeNode) treeReference;
	String srcval = src.getReference().getParameterValue(
		TestDesignTreeParameters.Critical.name());
	String destTreeval = thisTree.getReference().getParameterValue(
		TestDesignTreeParameters.Critical.name());

	if (srcval == null ? destTreeval != null : !srcval.equals(destTreeval)) {
	    return Comparable.DIFFERENT_PROPERTIES;
	}
	return compare(src.getObject());
    }

    private boolean hasSameStaticParameters(HashMap<String, Object> params,
	    Comparable treeTreference) {
	String description = (String) params.get("description");
	String critical = (String) params.get("critical");
	String val = ((TestDesignTreeNode) treeTreference).getReference()
		.getParameterValue(TestDesignTreeParameters.Critical.name());
	if ((val == null ? critical == null : val.equals(critical))
		&& getReference().getDescription().equals(description)) {
	    return true;
	} else {
	    return false;
	}

    }

    public boolean compareParameters(HashMap<String, Object> params,
	    Comparable treeTreference) {
	return hasSameStaticParameters(params, treeTreference);

    }

    public boolean hasSameRunTimeParams(HashMap<String, Object> params,
	    Comparable treeTreference) {
	return true;

    }
}
